package com.blackfish.command;

import java.util.List;

import com.blackfish.common.model.Active;
import com.blackfish.common.model.Command;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;


public class CommandService {
    public static final Command dao = new Command().dao();
    public static final Active activeDao = new Active().dao();

    public List<Command> findAll() {

        return dao.find("select * from command order by id asc");
    }

    public List<Command> findByStyle(String style) {
        return dao.find("select * from command where style=" + style + " order by id asc");
    }

    public List<Command> findByModule(String id) {
        return dao.find("select * from command where module_id=" + id + " order by id asc");
    }

    public Command findById(int id) {
        return dao.findById(id);
    }

    public void deleteById(int id) {
        dao.deleteById(id);
    }

    public boolean createCommand(int modId, String lable, String value, String active) throws Exception{

        int activeId = activeDao.findFirst("select * from active where name=\""+active+"\"or viewname=\""+active+"\"").getId();
        return new Command().set("module_id", modId).set("lable", lable).set("value", value).set("active", activeId).save();
    }

    public boolean saveCommand(int modId, String lable, String value, int active) {
        return new Command().set("module_id", modId).set("lable", lable).set("value", value).set("active", active).save();
    }

    public List<Record> findAllView() {
        return Db.find("SELECT c.id,a.viewname as active,c.lable,c.value,m.title FROM command as c INNER JOIN active as a on c.active=a.id INNER JOIN module as m ON m.id=c.module_id");
    }

}
